package com.sunshine.lnuplusweb.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间处理类
 */
public class DateUtils {
    //日历
    private static Calendar calendar = Calendar.getInstance();

    //今年
    private static int year = calendar.get(Calendar.YEAR);

    //本月
    private static int month = calendar.get(Calendar.MONTH) + 1;

    //开学日期
    private static String startDate = year + "-" + getStartMonth() + "-1";

    private static final long aWeekLong = 1000 * 60 * 60 * 24 * 7;

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public DateUtils() {

    }

    /**
     * 获取开学月份
     * 当当前月份小于8，默认为3月开学
     * 否则默认为9月开学
     */
    private static int getStartMonth() {
        return month < 8 ? 3 : 9;
    }

    /**
     * 获取开学时间Long值
     * @return  开学时间
     */
    public static long getStartDateLong() {
        try {
            return simpleDateFormat.parse(startDate).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取指定时间的long值
     */
    public static long getDateLong(String dateString) {
        try {
            return simpleDateFormat.parse(dateString).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取现在的long值
     * @return
     */
    public static long getNowLongTime() {
        return new Date().getTime();
    }

    /**
     * 获取指定周为第几周
     * @param thisDate
     * @return
     */
    public static int getThisWeek(long thisDate) {
        return (int) ((thisDate - getStartDateLong()) / aWeekLong + 1);
    }

    /**
     * 获取指定周为第几周
     * @param thisDate
     * @return
     */
    public static int getThisWeek(Date thisDate) {
        return (int) ((thisDate.getTime() - getStartDateLong()) / aWeekLong + 1);
    }

    /**
     * 返回今天所在的周为第几周
     * @return
     */
    public static int getThisWeek() {
        return (int) ((getNowLongTime() - getStartDateLong()) / aWeekLong + 1);
    }

    /**
     * 获取以周一开始的这天是一周的周几
     * @param day
     * @return
     */
    public static int getDayOfWeek(int day) {
        return day - 1 == 0 ? 7 : day - 1;
    }

}
